package com.heima.chengyuanbianliang;

import java.lang.reflect.Field;

public class ClassLocal04 {
    public static void main(String[] args) throws IllegalAccessException, InstantiationException, NoSuchFieldException, ClassNotFoundException {
        method1();
        method2();
    }
    private static void method2() throws ClassNotFoundException, NoSuchFieldException, InstantiationException, IllegalAccessException {
        //返回指定public的成员变量，并给成员变量赋值
        Class aClass = Class.forName("com.heima.chengyuanbianliang.Student");
        Field age = aClass.getField("age");
        Student o = (Student) aClass.newInstance();
        age.set(o,33);
        System.out.println(o);
    }

    private static void method1() throws ClassNotFoundException, NoSuchFieldException, InstantiationException, IllegalAccessException {
      //返回指定prviate的成员变量，获取成员变量的值
        Class aClass = Class.forName("com.heima.chengyuanbianliang.Student");
        Field phone = aClass.getDeclaredField("phone");
        phone.setAccessible(true);
        Student S = (Student) aClass.newInstance();
        Object o = phone.get(S);
        System.out.println(o);
    }
}
